﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Biblio2.LocalParsing
{
    public class LocalParseResult
    {
        #region Attributs
        #region Validity
        public ValidityEnum bParsingSuccess = ValidityEnum.Missing_Critical;
        private Dictionary<string, bool> m_dicoValidity;
        public String sException = String.Empty;
        public enum ValidityEnum
        {
            Complete,
            Missing_Optionnal,
            Missing_Critical,
        }
        #endregion Validity
        #region Values
        private PathPair m_pathPair;
        private Dictionary<string, string> m_dicoMatches;
        #endregion Values
        #region Process
        private bool m_bToBeProcessed = false;
        #endregion Process
        #region Const
        public const String csSerieName = "SerieName";
        public const String csSeasonIndex = "SeasonIndex";
        public const String csEpisodeIndex = "EpisodeIndex";
        public const String csEpisodeIndex2 = "EpisodeIndex2";
        public const String csEpisodeName = "EpisodeName";
        public const String csEpisodeFormat = "EpisodeFormat";
        public const String csEpisodeTeam = "EpisodeTeam";
        #endregion Const
        #endregion Attributs

        #region Constructeur
        public LocalParseResult(PathPair aPathPair)
        {
            m_pathPair = aPathPair;
            m_dicoMatches = new Dictionary<string, string>();
            m_dicoValidity = new Dictionary<string, bool>();
        }
        #endregion Constructeur

        #region Check
        public void CheckValidity()
        {
            LocalParseResult.CheckValidity(this);
        }

        private static void CheckValidity(LocalParseResult aResult)
        {
            aResult.sException = String.Empty;
            bool critical = false;
            bool optional = false;

            // Serie's Name
            if (aResult.m_dicoMatches.ContainsKey(csSerieName))
            {
                aResult.SetValidity(csSerieName, true);
            }
            else
            {
                aResult.SetValidity(csSerieName, false);
                critical = true;
                aResult.sException += "Serie non trouvée. ";
            }

            // Season
            if (aResult.m_dicoMatches.ContainsKey(csSeasonIndex))
            {
                try 
                {
                    Convert.ToInt32(aResult.m_dicoMatches[csSeasonIndex]);
                    aResult.SetValidity(csSeasonIndex, true);
                }
                catch (System.FormatException)
                {
                    aResult.SetValidity(csSeasonIndex, false);
                    critical = true;
                    aResult.sException += "Saison n'est pas un nombre. ";
                }
            }
            else
            {
                aResult.SetValidity(csSeasonIndex, false);
                critical = true;
                aResult.sException += "Saison non trouvée. ";
            }

            // Episode
            if (aResult.m_dicoMatches.ContainsKey(csEpisodeIndex))
            {
                try 
                {
                    Convert.ToInt32(aResult.m_dicoMatches[csEpisodeIndex]);
                    aResult.SetValidity(csEpisodeIndex, true);
                }
                catch (System.FormatException)
                {
                    aResult.SetValidity(csEpisodeIndex, false);
                    critical = true;
                    aResult.sException += "Episode n'est pas un nombre. ";
                }
            }
            else
            {
                aResult.SetValidity(csEpisodeIndex, false);
                critical = true;
                aResult.sException += "Episode non trouvé. ";
            }

            // Format
            if (aResult.m_dicoMatches.ContainsKey(csEpisodeFormat))
            {
                aResult.SetValidity(csEpisodeFormat, true);
            }
            else
            {
                aResult.SetValidity(csEpisodeFormat, false);
                optional = true;
                aResult.sException += "Format non trouvé. ";
            }

            // Team
            if (aResult.m_dicoMatches.ContainsKey(csEpisodeTeam))
            {
                aResult.SetValidity(csEpisodeTeam, true);
            }
            else
            {
                aResult.SetValidity(csEpisodeTeam, false);
                optional = true;
                aResult.sException += "Team non trouvé. ";
            }

            if (critical)
                aResult.bParsingSuccess = ValidityEnum.Missing_Critical;
            else if (optional)
                aResult.bParsingSuccess = ValidityEnum.Missing_Optionnal;
            else
                aResult.bParsingSuccess = ValidityEnum.Complete;
            
        }


        #endregion Static Check

        #region Accesseurs
        public PathPair PathPair { get { return m_pathPair; } }
        public bool ToBeProcessed
        {
            get { return m_bToBeProcessed; }
            set { m_bToBeProcessed = value; }
        }

        #endregion Accesseurs

        #region Public
        public string GetValue(string aField)
        {
            if (m_dicoMatches.ContainsKey(aField))
                return m_dicoMatches[aField];
            else
                return string.Empty;
        }

        public void SetValue(string aField, string aValue)
        {
            if (m_dicoMatches.ContainsKey(aField))
                m_dicoMatches[aField] = aValue;
            else
                m_dicoMatches.Add(aField, aValue);
        }

        public bool GetValidity(string aField)
        {
            if (m_dicoValidity.ContainsKey(aField))
                return m_dicoValidity[aField];
            else
                return false;
        }

        public void SetValidity(string aField, bool aValidity)
        {
            if (m_dicoValidity.ContainsKey(aField))
                m_dicoValidity[aField] = aValidity;
            else
                m_dicoValidity.Add(aField, aValidity);
        }
        #endregion Public
    }
}
